/*
 * Copyright 2006-2008 Michele Mostarda ( michele.mostarda@gmail.com ).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.graphnav.swing;

import org.graphnav.swing.event.EditingListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * This class defines a default renderable arc editor.
 */
public class DefaultArcRenderableEditor extends RenderableEditor {

    private JLabel putNameLabel = new JLabel("Arc Name");
    private JTextField putNameTextField = new JTextField(20);

    private JButton okButton = new JButton("OK");
    private JButton cancelButton = new JButton("Cancel");
    private JButton deleteButton = new JButton("Delete");

    private DefaultArc edited = null;
    private EditingListener editingListener = null;

    public DefaultArcRenderableEditor() {
        setLayout(new FlowLayout());

        okButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                edited.setDescription(putNameTextField.getText());
                editingListener.editingSucceded();
            }
        });

        cancelButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editingListener.editingRejected();
            }
        });

        deleteButton.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                editingListener.deletionSucceded();
            }
        });

        add(putNameLabel);
        add(putNameTextField);
        add(okButton);
        add(cancelButton);
        add(deleteButton);
    }

    private void clear() {
        putNameTextField.setText("");
    }

    protected void configure(DefaultArc da, EditingListener el) {
        edited = da;
        editingListener = el;
        putNameTextField.setText(da.getDescription());
    }

    public void showEditor() {
        setVisible(true);
    }
}
